package com.jgp.third.pay.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.common.utils.JGPUtil;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Result;
import com.jgp.third.pay.model.PaymentConfig;
import com.jgp.third.pay.pojo.AliPaymentParam;
import com.jgp.third.pay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/10/24
 */
@RestController
@RequestMapping("/third/pay/paymentConfigApiController")
public class PaymentConfigApiController extends JGPBackController {
    
    @Autowired
    private PayService payService;
    
    
    @RequestMapping("queryPayments")
    public Result queryPayments() {
        return ajaxRe(true).addData("payments",payService.queryAllPaymentsForCurrentUser());
    }
    
    @RequestMapping("queryAllPayments")
    public Result queryAllPayments() {
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("支付类型","mode","SYS.DROP.PAYMENT.TYPE"));
        categories.add(new GridResult.Category("支付说明","remark"));
        return ajaxReGrid("gdata",categories,payService.queryAllPaymentsForCurrentUser());
    }
    
    @RequestMapping("savePaymentConfig")
    public Result saveAliPaymentConfig(@UIParam("paymentConfig") PaymentConfig config,
                                       @UIParam("aliPaymentParam") AliPaymentParam param) {
        
        if(Objects.isNull(config.getId())){
            boolean flag = payService.checkPaymentModeUniqueForCurrentUser(config.getMode());
            return ajaxRe(!flag).addMsg("支付方式已经存在！！");
        }else{
            payService.savePaymentConfig(config, param);
            return ajaxRe(true);
        }
    }
    
    @RequestMapping("queryPaymentConfigById")
    public Result queryPaymentConfigById(String configId) {
        PaymentConfig config = payService.queryPaymentConfigById(configId);
        AliPaymentParam param = JGPUtil.toBean(config.getConfig(), AliPaymentParam.class);
        return ajaxReForm("fdata",config,param);
    }
    
    
}
